package com.sstir.doctor.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * @ClassName BigdecimalUtil
 * @Description 结果保留两位小数
 * @Author hanjunjun
 * @Date 2020/6/4 15:23
 */
@Component
public class BigdecimalUtil {

    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.00");

    /**
     * 文件数据单位换算的进率
     */
    private static final String DATA_RADIO = "1024";

    private static final String ZERO = "0";

    private static final String ONE = "1";

    public static BigDecimal add(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.add(b2);
    }

    public static BigDecimal subtract(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.subtract(b2);
    }

    /**
     * 两数相乘
     *
     * @param s1
     * @param s2
     * @return java.math.BigDecimal
     **/
    public static BigDecimal multiply(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.multiply(b2);
    }

    public static BigDecimal divide(String s1, String s2) {
        if (0 == Double.parseDouble(s2)) {
            throw new ArithmeticException("除数为0");
        }
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.divide(b2);
    }

    /**
     * 比较两个金额字符串是否相等
     * 3和3.00是相等的
     *
     * @param s1
     * @param s2
     * @return boolean
     **/
    public static boolean equal(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.compareTo(b2) == 0;

    }

    /**
     * 保留两位小数
     *
     * @param s
     * @return java.lang.String
     **/
    public static String parseAmount(String s) {
        return DECIMAL_FORMAT.format(new BigDecimal(s));
    }

    /**
     * 数据计算业务传参
     */
    @Getter
    @Setter
    @NoArgsConstructor
    public static class DataCalculateBO {
        /**
         * 大小
         */
        private String size;
        /**
         * 单位
         */
        private String unit;
    }


    /**
     * 单位转换为byte
     *
     * @return java.lang.String
     * @author hjj
     **/
    public static String transformDataSizeToByte(String size, String unit) {
        unit = unit.toUpperCase();
        switch (unit) {
            case "K": {
                size = multiply(size, DATA_RADIO).toString();
                break;
            }
            case "M": {
                size = multiply(size, multiply(DATA_RADIO, DATA_RADIO).toString()).toString();
                break;
            }
            case "G": {
                size = multiply(size, multiply(DATA_RADIO, multiply(DATA_RADIO, DATA_RADIO).toString()).toString()).toString();
                break;
            }
            case "T":{
                //化成G
                String sizeG = multiply(size, DATA_RADIO).toString();
                size = transformDataSizeToByte(sizeG, "G");
                break;
            }
            default: {
                break;
            }
        }
        size = parseAmount(size);
        if (size.contains(".") && size.endsWith(ZERO)) {
            size = size.substring(0, size.length() - 1);
            if (size.endsWith(ZERO)) {
                size = size.substring(0, size.indexOf("."));
            }
        }
        return size;
    }

    /**
     * 将单位为B的数据大小换算为合适的单位数值供展示
     * 保留2位小数
     *
     * @param dataSize 单位为B的数据大小
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author hjj
     **/
    public static DataCalculateBO transformDataSizeDisplay(String dataSize) {
        String unit = StringUtils.EMPTY;
        DataCalculateBO bo = new DataCalculateBO();
        //如果传入0  直接返回
        if (ZERO.equals(dataSize)){
            bo.setSize("0");
            bo.setUnit("B");
            return bo;
        }
        String dataKb = divide(dataSize, DATA_RADIO).toString();
        //如果得到的商值大于等于1
        if (greaterOrEqual(dataKb, ONE)) {
            dataSize = dataKb;
            unit = "K";
            String dataMb = divide(dataSize, DATA_RADIO).toString();
            if (greaterOrEqual(dataMb, ONE)) {
                dataSize = dataMb;
                unit = "M";
                String dataGb = divide(dataSize, DATA_RADIO).toString();
                if (greaterOrEqual(dataGb, ONE)) {
                    dataSize = dataGb;
                    unit = "G";
                    String dataTb = divide(dataSize, DATA_RADIO).toString();
                    if (greaterOrEqual(dataTb, ONE)){
                        dataSize = dataTb;
                        unit = "T";
                    }
                }
            }
        } else {
            unit = "B";
        }
        //保留两位小数
        dataSize = parseAmount(dataSize);
        //去除最后一位的0
        if (dataSize.contains(".") && dataSize.endsWith(ZERO)) {
            dataSize = dataSize.substring(0, dataSize.length() - 1);
            if (dataSize.endsWith(ZERO)){
                dataSize = dataSize.substring(0, dataSize.indexOf("."));
            }
        }

        bo.setSize(dataSize);
        bo.setUnit(unit);
        return bo;
    }

    /**
     * 比较两个数的大小
     *
     * @param s1
     * @param s2
     * @return int  1:s1>s2  0:s1=s2  -1:s1<s2
     * @author hjj
     **/
    public static int compare(String s1, String s2) {
        return new BigDecimal(s1).compareTo(new BigDecimal(s2));
    }

    /**
     * 判断s1是不是大于等于s2
     *
     * @param s1
     * @param s2
     * @return boolean
     * @author hjj
     **/
    public static boolean greaterOrEqual(String s1, String s2) {
        if (1 == compare(s1, s2)) {
            return true;
        } else {
            return 0 == compare(s1, s2);
        }

    }


    public static void main(String[] args) {

        System.out.println(JSONObject.toJSONString(transformDataSizeDisplay("0")));

    }

}
